package com.sayice.gomoku.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * JDBC 工具类
 */
public class JdbcUtils {
    /**
     * 数据库连接池
     */
    private static DruidDataSource dataSource;
    
    /**
     * 线程本地连接
     */
    private static final ThreadLocal<Connection> localConn = new ThreadLocal<>();
    
    static {
        Properties properties = new Properties();
        // 读取 jdbc.properties属性配置文件
        InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
        if (in == null) {
            System.out.println("jdbc.properties 配置文件打开错误！");
        } else {
            try {
                // 从流中加载数据
                properties.load(in);
                // 创建数据库连接池
                dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    /**
     * 获取数据库连接池中的连接
     *
     * @return 获取连接成功则返回 Connection 对象，否则返回 null
     */
    public static Connection getConnection() {
        Connection conn = localConn.get();
        if (conn == null) {
            try {
                // 从数据库连接池中获取连接
                conn = dataSource.getConnection();
                // 保存到 ThreadLocal 对象中，供后面的 jdbc 操作使用
                localConn.set(conn);
                // 设置为手动管理事务
                conn.setAutoCommit(false);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return conn;
    }
    
    /**
     * 提交事务，并关闭连接
     */
    public static void commitAndClose() {
        Connection conn = localConn.get();
        if (conn != null) {
            try {
                // 提交事务
                conn.commit();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    // 关闭连接
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        localConn.remove();
    }
    
    /**
     * 回滚事务，并关闭连接
     */
    public static void rollbackAndClose() {
        Connection conn = localConn.get();
        if (conn != null) {
            try {
                // 回滚事务
                conn.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    // 关闭连接
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        localConn.remove();
    }
}